/**
 * Created by kangm_000 on 2016/11/30 0030.
 */
var vm = new Vue({
    el: '#main',
    data: {
        menus: [],
        currentPage: "回溯管理",
        user:{
        	operator:{}
        }
    },//改变分数的函数
    created: function () {
        $("#mainFrame").attr("src",App.getBaseUrl() + "sys/track/nrrback.html");
        this.getUserAuth();
    },
    computed: {
        filterRootMenus: function () {
            var self = this;
            return self.menus.filter(function (menu) {
                return menu.parentNo == "1";
            })
        }
    },
    methods: {
    	getUserAuth:function(){
    		App.ajax({
    			type:"GET",
    			url:App.getApiUrl("queryUserAuthURL"),
    			success:function(data){
					vm.user = data;
                    //TODO 测试时注释
                    vm.menus = data.menuList;
				},
				error:function(){
					window.location.href = App.getBaseUrl() + "login.html";
				}
			});
        },
        childMenus:function(menus,parentNo){
            return menus.filter(function (menu) {
                return menu.parentNo == parentNo;
            })
        },
        parentClick:function(e){
        	$(e.target).siblings('.sidebar-nav-sub').slideToggle(80)
                .end()
                .find('.sidebar-nav-sub-ico').toggleClass('sidebar-nav-sub-ico-rotate');
        },
        menuChange: function (text, url) {
            if(text && text != this.currentPage){
                this.currentPage = text;
                changePage(App.getBaseUrl() + url);
            }
            closeLeftNav();
        },
        logout: function () {
        	showConfirm(
            	"提示信息",
            	"确定要退出系统吗？",
            	function(){
            		App.ajax({
                        type: "GET",
                        url: App.getApiUrl("quitUserURL"),
                        success:function(){
                        	window.location.href = App.getBaseUrl() + "login.html";
                        }
                    });
            	}	
            );
        }

    }
});
//设置当前选中菜单，供子页面调用
function selectMenu(menu) {
	if(vm.currentPage =="用户管理"){
		vm.currentPage = menu;
	}
}
//切换页面
function changePage(url) {
    $("#mainFrame").attr("src",url);
}

//切换页面时，页面滚动到顶部
function scrollToTop(){
	$(window).scrollTop(0);
}


//显示弹框确认页面
function showPrompt(confirm,cancel){	
	var $confirm = $('#my-prompt');
	var $confirmBtn = $confirm.find('[data-am-modal-confirm]');
	var $cancelBtn = $confirm.find('[data-am-modal-cancel]');
	$confirmBtn.off('click.confirm.modal.amui').unbind("click"); //移除click
	$confirmBtn.off('click.cancel.modal.amui').unbind("click"); //移除click
	$confirmBtn.off('click.confirm.modal.amui').on('click', function() {
		if(confirm){
     	   confirm();
        }
	});

	$cancelBtn.off('click.cancel.modal.amui').on('click', function() {
		if(cancel){
      	  cancel();
        }
	});
	
	$('#my-prompt').modal({
        relatedTarget: this,
    });
}

//显示弹框确认页面
function showConfirm(title,text,confirm,cancel){
	if(title){
		$("#my-confirm").find(".am-modal-hd").html(title);
	}
	if(text){
		$("#my-confirm").find(".am-modal-bd").html(text);
	}
	
	var $confirm = $('#my-confirm');
	var $confirmBtn = $confirm.find('[data-am-modal-confirm]');
	var $cancelBtn = $confirm.find('[data-am-modal-cancel]');
	$confirmBtn.off('click.confirm.modal.amui').unbind("click"); //移除click
	$confirmBtn.off('click.cancel.modal.amui').unbind("click"); //移除click
	$confirmBtn.off('click.confirm.modal.amui').on('click', function() {
		if(confirm){
     	   confirm();
        }
	});

	$cancelBtn.off('click.cancel.modal.amui').on('click', function() {
		if(cancel){
      	  cancel();
        }
	});
	
	$('#my-confirm').modal({
        relatedTarget: this,
    });
}
function closeConfirm(id){
	$('#' + id).modal('close');
}

//显示弹框确认页面
function showConfirmOne(confirm,cancel){

	
	var $confirm = $('#my-confirmOne');
	var $confirmBtn = $confirm.find('[data-am-modal-confirm]');
	var $cancelBtn = $confirm.find('[data-am-modal-cancel]');
	$confirmBtn.off('click.confirm.modal.amui').unbind("click"); //移除click
	$confirmBtn.off('click.cancel.modal.amui').unbind("click"); //移除click
	$confirmBtn.off('click.confirm.modal.amui').on('click', function() {
		if(confirm){
     	   confirm();
        }
	});

	$cancelBtn.off('click.cancel.modal.amui').on('click', function() {
		if(cancel){
      	  cancel();
        }
	});
	
	$('#my-confirmOne').modal({
        relatedTarget: this,
    });
}
//显示弹框确认页面
function showConfirmTwo(){
	$('#my-confirmTwo').modal({
        relatedTarget: this,
    });
}
//显示警告框
function showAlert(text,callback){
	if(text){
		$("#my-alert").find(".am-modal-bd").html(text);
	}
	var $confirm = $('#my-alert');
	var $confirmBtn = $confirm.find('[data-am-modal-confirm]');
	$confirmBtn.off('click.confirm.modal.amui').unbind("click"); //移除click
	$confirmBtn.off('click.confirm.modal.amui').on('click', function() {
		if(callback){
        	callback();
        }
	});

	
	$('#my-alert').modal({
        relatedTarget: this,
    });
}

/**
 * 调整iframe高度
 */
function reinitIframe(){
    var iframe = document.getElementById("mainFrame");
    try {
        //改变子页面样式
        var height = iframe.contentWindow.getContentHeight();
        iframe.height = height;
    } catch (ex) {

    }
}
window.setInterval("reinitIframe()", 200);